home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 347_01.zip / EXAMPLE4.C < prev    next >
C/C++ Source or Header  |  1993-04-13  |  3KB  |  145 lines

  1. /*
  2.  *  program Example4
  3.  *  purpose: Test shell for threaded AVL-tree. Demonstrates alternate
  4.  *           identifier functions, alternate compare functions.
  5.  *
  6.  *  Usage:  EXAMPLE4 <filename>
  7.  *
  8.  *  Copyright (c) 1991  Bert C. Hughes
  9.  *                      200 N. Saratoga
  10.  *                      St.Paul, MN 55104
  11.  *                      Compuserve 71211,577
  12.  */
  13.  
  14. #if defined NDEBUG
  15.      !*&^%$!!@@@()^H??||~:~:;:=+
  16.      File "Example4.c" MUST be compiled with NDEBUG undefined,
  17.      so that the "assert()" macro/function will be included.
  18. #endif
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <assert.h>
  24. #include "tavltree.h"
  25.  
  26. #if defined __TURBOC__
  27. #include <conio.h>
  28. #else
  29. #define cprintf printf
  30. #endif
  31.  
  32. void pause(void)
  33. {
  34.     int c;
  35.     cprintf("\r\nPress <ENTER> to continue...");
  36.     while (getchar() != '\n');
  37.     cprintf("\r\n");
  38. }
  39.  
  40. typedef struct datarecord {
  41.           long  EmpID;
  42.           long  salary;
  43.           char  Name[20];
  44.         } DataRecord, *DRptr;
  45.  
  46.  
  47. void *NameID(void *x)
  48. {
  49.     DRptr rec = x;
  50.     return (rec->Name);
  51. }
  52.  
  53. void *Employee(void *x)
  54. {
  55.     DRptr rec = x;
  56.     return (&rec->EmpID);
  57. }
  58.  
  59. int IDcmp(void *x1, void *x2)
  60. {
  61.     DRptr x = x1;
  62.     DRptr y = x2;
  63.     return (x->EmpID > y->EmpID ? -1 : (x->EmpID < y->EmpID ? +1 : 0));
  64. }
  65.  
  66.  
  67. void *copy_rec(const void *x)
  68. {
  69.     DRptr p = malloc(sizeof(DataRecord));
  70.     if (p)
  71.         memcpy(p,x,sizeof(DataRecord));
  72.     return(p);
  73. }
  74.  
  75. void *read_rec(void *dest, const void *src)
  76. {
  77.     return(memcpy(dest,src,sizeof(DataRecord)));
  78. }
  79.  
  80. main (int argc, char *argv[])
  81. {
  82.     TAVL_treeptr tree1, *tree2;
  83.     TAVL_nodeptr p;
  84.     DataRecord InpR, *Dta;
  85.  
  86.     int (*cmp)() = strcmp;  /* use C library function "strcmp" to compare */
  87.                             /* DataRecord.Name identifiers */
  88.  
  89.     int (*numcmp)() = IDcmp; /* Employee Idenitifier */
  90.  
  91.     FILE *fp;
  92.  
  93. #if defined __TURBOC__
  94.     directvideo = 0;   /* change to 1 if there is "snow" on your monitor */
  95. #endif
  96.  
  97.     if (argc < 2)
  98.     assert(fp = fopen("EMPLDATA","r"));
  99.     else
  100.     assert(fp = fopen(argv[1],"r"));
  101.  
  102.     assert((tree1 = tavl_init(cmp,NameID,copy_rec,free,read_rec,malloc,free)));
  103.     assert((tree2 = tavl_init(numcmp,Employee,copy_rec,free,read_rec,malloc,free)));
  104.  
  105.     cprintf("Input will be echoed...\r\n");
  106.  
  107.     while (fscanf(fp,"%ld %ld %s",&InpR.EmpID,&InpR.salary,InpR.Name) != EOF) {
  108.     /* echo input */
  109.     cprintf("%-10.ld %-10ld %+20s\r\n",InpR.EmpID,InpR.salary,InpR.Name);
  110.     /* insert into trees */
  111.     if (tavl_insert(tree1,&InpR,0) == NULL) {
  112.         cprintf("\r\nOut of memory!");
  113.         exit(1);
  114.     }
  115.     if (tavl_insert(tree2,&InpR,0) == NULL) {
  116.         cprintf("\r\nOut of memory!");
  117.         exit(1);
  118.     }
  119.     } /* end while - input loop */
  120.  
  121.     fclose(fp);
  122.  
  123.     pause();
  124.  
  125.     cprintf("Listed by NAME\r\n");
  126.  
  127.     p = tavl_reset(tree1);
  128.  
  129.     while (p = tavl_succ(p)) {
  130.     Dta = p->dataptr;
  131.     cprintf("%ld    %-23.20s   %ld\r\n",Dta->EmpID,Dta->Name,Dta->salary);
  132.     }
  133.  
  134.     pause();
  135.  
  136.     cprintf("Listed by Employee_ID (descending)\r\n");
  137.  
  138.     p = tavl_reset(tree2);
  139.  
  140.     while (p = tavl_succ(p)) {
  141.     Dta = p->dataptr;
  142.     cprintf("%ld    %-23.20s   %ld\r\n",Dta->EmpID,Dta->Name,Dta->salary);
  143.     }
  144. }
  145.